<?php

App::uses('AppController', 'Controller');

/**
 * Sliders Controller
 *
 * @property Slider $Slider
 */
class SlidersController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Slider->recursive = 0;
        $this->set('sliders', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->Slider->id = $id;
        if (!$this->Slider->exists()) {
            throw new NotFoundException(__('Invalid slider'));
        }
        $this->set('slider', $this->Slider->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->loadModel('Image');
            $this->Image->updateMeioUploadSize('Slider', array('Slider' => array('width' => $this->request->data['Slider']['width'],
                    'height' => $this->request->data['Slider']['height'])));
            $this->Slider->create();
            if ($this->Slider->saveAssociated($this->request->data)) {
                $this->Session->setFlash(__('The slider has been saved'), 'default', array(), 'good');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The slider could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Slider->id = $id;
        if (!$this->Slider->exists()) {
            throw new NotFoundException(__('Invalid slider'));
        }

        if ($this->request->is('post') || $this->request->is('put')) {
            $this->loadModel('Image');
            $this->Image->updateMeioUploadSize('Slider', array('Slider' => array('width' => $this->request->data['Slider']['width'],
                    'height' => $this->request->data['Slider']['height'])));
            if ($this->Slider->saveAssociated($this->request->data)) {
                $this->Session->setFlash(__('The slider has been saved'), 'default', array(), 'good');
                #$this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The slider could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        }
        $this->request->data = $this->Slider->read(null, $id);
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Slider->id = $id;
        if (!$this->Slider->exists()) {
            throw new NotFoundException(__('Invalid slider'));
        }
        if ($this->Slider->delete()) {
            $this->Session->setFlash(__('Slider deleted'), 'default', array(), 'good');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Slider was not deleted'), 'default', array(), 'bad');
        $this->redirect(array('action' => 'index'));
    }
}
